<template>
    <div style="width: 500px">
        <el-form :model="info" label-width="120px">
            <el-form-item label="用户名">
                <el-input v-model="info.username" disabled />
            </el-form-item>
            <el-form-item label="学号">
                <el-input v-model="info.schoolId" />
            </el-form-item>
            <el-form-item label="姓名">
                <el-input v-model="info.name" />
            </el-form-item>
            <el-form-item label="性别">
                <el-radio-group v-model="info.isMale">
                    <el-radio label="1">男</el-radio>
                    <el-radio label="0">女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="手机号">
                <el-input v-model="info.phoneNumber" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save">保存</el-button>
                <el-button @click="clear">清空</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import cookie from 'js-cookie'
import { getInfo, setInfo } from '@/http/api/user'

export default {
    name: 'LoginView',
    data() {
        return {
            username: '',
            info: {
                username: '',
                schoolId: '',
                name: '',
                isMale: '',
                phoneNumber: ''
            }
        }
    },
    methods: {
        async save() {
            let res = await setInfo(this.info)
            if (res.code !== 0) {
                this.$message.warning(res.msg)
                return
            }
            this.$message.success('修改成功')
        },
        clear() {
            this.info.schoolId = ''
            this.info.name = ''
            this.info.isMale = ''
            this.info.phoneNumber = ''
        }
    },
    async mounted() {
        this.username = cookie.get('username')
        let res = await getInfo({
            username: this.username
        })
        if (res.code !== 0) {
            this.$message.warning(res.msg)
            return
        }
        this.info = res.data
        this.info.isMale = '' + this.info.isMale
    }
}
</script>